package com.example.mymicaihu.adapter.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.mymicaihu.R;
import com.example.mymicaihu.bean.home.NewsBean;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class NewsAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<NewsBean.NewsListBean> list;

    public NewsAdapter(Context context, List<NewsBean.NewsListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_home_news1, parent, false);
            return new ViewHolder(view);
        } else if (viewType == 2) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_home_news2, parent, false);
            return new ViewHolder1(view);
        }
        View view = LayoutInflater.from(context).inflate(R.layout.item_home_news3, parent, false);
        return new ViewHolder3(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        if (type == 1) {
            ViewHolder holder1 = (ViewHolder) holder;
            holder1.title1.setText(list.get(position).getArticleTitle());
            holder1.time1.setText(list.get(position).getArticleTime());
            holder1.commentNum1.setText(list.get(position).getCommentNum() + "评论");
            Glide.with(context).load(list.get(position).getArticleThumb().get(0)).into(holder1.imageView1);
        } else if (type == 2) {
            ViewHolder1 holder2 = (ViewHolder1) holder;
            holder2.title2.setText(list.get(position).getArticleTitle());
            holder2.time2.setText(list.get(position).getArticleTime());
            holder2.commentNum2.setText(list.get(position).getCommentNum() + "评论");
            Glide.with(context).load(list.get(position).getArticleThumb().get(0)).into(holder2.imageView21);
            Glide.with(context).load(list.get(position).getArticleThumb().get(1)).into(holder2.imageView22);
            Glide.with(context).load(list.get(position).getArticleThumb().get(2)).into(holder2.imageView23);
        } else {
            ViewHolder3 holder3 = (ViewHolder3) holder;
            holder3.TvTuijian3.setText(list.get(position).getArticleTitle());
            holder3.stan.setUp(list.get(position).getVideoUrl(), true, list.get(position).getArticleTitle());
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position).getArticleType().equals("1") && list.get(position).getArticleThumb().size() == 1) {
            return 1;
        } else if (list.get(position).getArticleType().equals("1") && list.get(position).getArticleThumb().size() > 1) {
            return 2;
        }
//        else if (list.get(position).getArticleType().equals("3")&&list.get(position).getArticleThumb().size()>1) {
        return 3;
    }

    public static
    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_item_home_news1_title)
        TextView title1;
        @BindView(R.id.tv_item_home_news1_time)
        TextView time1;
        @BindView(R.id.tv_item_home_news1_commentNum)
        TextView commentNum1;
        @BindView(R.id.iv_item_home_news1)
        ImageView imageView1;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static
    class ViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_item_home_news2_title)
        TextView title2;
        @BindView(R.id.tv_item_home_news2_time)
        TextView time2;
        @BindView(R.id.tv_item_home_news2_commentNum)
        TextView commentNum2;
        @BindView(R.id.iv_item_home_news21)
        ImageView imageView21;
        @BindView(R.id.iv_item_home_news22)
        ImageView imageView22;
        @BindView(R.id.iv_item_home_news23)
        ImageView imageView23;

        ViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static
    class ViewHolder3 extends RecyclerView.ViewHolder {
        @BindView(R.id.Tv_tuijian_3)
        TextView TvTuijian3;
        @BindView(R.id.stan)
        StandardGSYVideoPlayer stan;

        ViewHolder3(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
